package com.ctshk.rpc.finance.mapper;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.rpc.finance.base.CustomBaseMapper;
import com.ctshk.rpc.finance.dto.paidin.PaidInAmountCountDTO;
import com.ctshk.rpc.finance.dto.paidin.PaidInsupplierOrderListDTO;
import com.ctshk.rpc.finance.entity.FinancePaidInDetail;
import com.ctshk.rpc.finance.req.paidin.PaidInsupplierOrderListReq;
import com.ctshk.rpc.finance.req.supplier.SupplierReconciliationAmountDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 财务实付供应商对账单明细 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-04-12
 */
public interface FinancePaidInDetailMapper extends CustomBaseMapper<FinancePaidInDetail> {

    /**
     * 币种金额统计
     * @param id
     * @return
     */
    @Select("SELECT currency_name,(SUM(if(order_type=1,payable_amount,0)) - SUM(if(order_type=2,payable_amount,0))) AS totalPayableAmount,\n" +
            "(SUM(if(order_type=1,pay_amount,0)) - SUM(if(order_type=2,pay_amount,0))) AS totalPayAmount,\n" +
            "(SUM(if(order_type=1,unpay_amount,0)) - SUM(if(order_type=2,unpay_amount,0))) AS totalUnPayAmount\n" +
            "FROM t_finance_paid_in_detail\n" +
            "WHERE paid_in_id = #{paid_in_id} AND is_deleted = 0\n" +
            "GROUP BY currency_id")
    List<PaidInAmountCountDTO> sumAmountByCurrency(@Param("paid_in_id") Long id);

    /**
     * 批量删除
     * @param id
     * @param userId
     * @return
     */
    @Update("UPDATE t_finance_paid_in_detail\n" +
            "SET is_deleted = 1,modified_id = #{user_id}\n" +
            "WHERE paid_in_id = #{paid_in_id} AND is_deleted = 0")
    Integer cleanByPaidIn(@Param("paid_in_id")Long id, @Param("user_id")Long userId);

    /**
     * 供应商往来预订单列表
     * @param req
     * @param iPage
     * @return
     */
    @Select("<script>SELECT a.*,b.expect_pay_time\n" +
            "FROM t_finance_paid_in_detail a\n" +
            "LEFT JOIN t_finance_supplier_reconciliation b ON a.reconciliation_id = b.id\n" +
            "WHERE a.is_deleted = 0\n" +
            "<if test=\"req.createTimeBegin != null and req.createTimeEnd != null\">AND a.gmt_create BETWEEN #{req.createTimeBegin} AND #{req.createTimeEnd}</if> \n" +
            "<if test=\"req.reservationType != null\">AND a.reservation_type = #{req.reservationType}</if> \n" +
            "<if test=\"req.currencyId != null\">AND a.currency_id = #{req.currencyId}</if> \n" +
            "<if test=\"req.supplierName != null and req.supplierName != ''\">AND a.supplier_name LIKE CONCAT('%',#{req.supplierName},'%') </if> \n" +
            "<if test=\"req.productNumber != null and req.productNumber != ''\">AND a.product_number LIKE CONCAT('%',#{req.productNumber},'%') </if> \n" +
            "<if test=\"req.orderNumber != null and req.orderNumber != ''\">AND a.order_number LIKE CONCAT('%',#{req.orderNumber},'%') </if> \n" +
            "<if test=\"req.payType != null\">AND a.pay_type = #{req.payType}</if> \n" +
            "<if test=\"req.createId != null\">AND a.create_id = #{req.createId}</if> \n" +
            "<if test=\"req.payStatus != null\">AND b.pay_status = #{req.payStatus}</if> \n" +
            "ORDER BY a.gmt_create DESC</script>")
    Page<PaidInsupplierOrderListDTO> querySupplierOrderListByCondition(@Param("req") PaidInsupplierOrderListReq req, @Param("iPage") Page<PaidInsupplierOrderListDTO> iPage);


    /**
     * 获取供应商预订单最新付款记录
     * @param orderId
     * @return
     */
    @Select("SELECT * \n" +
            "FROM t_finance_paid_in_detail a\n" +
            "LEFT JOIN t_finance_paid_in b ON a.paid_in_id = b.id\n" +
            "WHERE a.order_id = #{order_id} AND b.is_deleted = 0 AND b.pay_status != 2 ORDER BY a.gmt_create LIMIT 1")
    FinancePaidInDetail getLastestOne(@Param("order_id") Long orderId);

    /**
     *
     * @param idList
     * @return
     */
    @Select("SELECT order_id,pay_status,payable_amount,paid_amount,unpay_amount\n" +
            "FROM t_finance_paid_in_detail\n" +
            "WHERE is_deleted = 0 AND order_id IN \n" +
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "GROUP BY order_id ORDER BY gmt_create DESC")
    List<SupplierReconciliationAmountDTO> selectByOrderIdList(@Param("list") List<Long> idList);
}
